<?php

namespace app\api\controller\driver;

use app\common\constant\driving\DriverConstant;
use app\common\controller\Api;
use app\common\service\driver\DriverService;
use think\Validate;

/**
 * 司机端-我的接口
 * @ApiWeigh (88)
 */
class User extends Api
{
    protected $beforeActionList = [
        'mobilelogin_before' => ['only' => 'mobilelogin'],
        'location_before' => ['only' => 'location'],
        'feedback_before' => ['only' => 'feedback'],
    ];
    protected $noNeedLogin = ['mobilelogin','version'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiInternal
     */
    public function mobilelogin_before()
    {
        $params = $this->request->post();
        $rule = [
            'mobile|手机号' => 'require|gt:0',
            'captcha|验证码' => 'require|gt:0',
        ];
        $message = [];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
    }

    /**
     * @ApiInternal
     */
    public function location_before()
    {
        $params = $this->request->post();
        $rule = [
            'lng|经度' => 'require|gt:0',
            'lat|纬度' => 'require|gt:0',
        ];
        $message = [];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
    }

    /**
     * @ApiInternal
     */
    public function feedback_before()
    {
        $params = $this->request->post();
        $rule = [
            'mobile|手机号' => 'require|regex:^1\d{10}$',
            'content|内容' => 'require',
        ];
        $message = [
            'mobile.regex' => '手机格式不正确',
        ];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
    }


    /**
     * 手机验证码登录
     *
     * @ApiMethod (POST)
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams (name="captcha", type="string", required=true, description="验证码")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn({"code":1,"msg":"登录成功","time":"1720508038","data":{"id":1,"code":"工号","username":"张三","nickname":"昵称","mobile":"手机号","avatar":"头像","level":"等级 星级","money":"余额","score":"代驾分","createtime":1720508038,"updatetime":1720508038,"status":"状态:normal=正常,frozen=禁用","is_online":"在线状态:0=下线,1=在线","year":"驾龄","order_num":"代驾次数","token":"707c0961-40a4-456f-82d6-b046b384edc0","user_id":1,"expiretime":1723100038,"type":1,"expires_in":2592000}})
     */
    public function mobilelogin()
    {
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error('手机格式不正确');
        }
//        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
//            $this->error('验证码不正确');
//        }
        $user = \app\common\model\driving\Driver::getByMobile($mobile);
        if (!$user) {
            $this->error('账户不存在');
        }
        if ($user->status != 'normal') {
            $this->error('账户已经被锁定');
        }
        //如果已经有账号则直接登录
        $ret = $this->auth->direct_new($user, 1);
        if ($ret) {
//            Sms::flush($mobile, 'mobilelogin');
            $data = $this->auth->getUserinfo();
            $this->success('登录成功', $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 个人中心
     *
     * @ApiMethod (POST)
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn({"code":1,"msg":"个人中心","time":"1720508038","data":{"id":1,"code":"工号","username":"张三","nickname":"昵称","mobile":"手机号","avatar":"头像","level":"等级 星级","money":"可提现余额","score":"代驾分","createtime":1720508038,"updatetime":1720508038,"status":"状态:normal=正常,frozen=禁用","is_online":"在线状态:0=下线,1=在线","year":"驾龄","order_num":"代驾次数","frozen_money":"冻结金额","is_recharge": "是否开启充值按钮:0=关闭,1=开启","order_receive_money": "20","order_receive_money_text": "当余额少于20元时无法接单"}})
     */
    public function index()
    {
        $user = $this->auth->getUser();
        $this->success('个人中心', DriverService::index($user));
    }

    /**
     * 开启/关闭听单
     *
     * @ApiMethod (POST)
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code": 1,"msg": "开启成功","time": "1694660842","data": null})
     **/
    public function open()
    {
        $user = $this->auth->getUser();
        $user->is_online = $user->is_online == DriverConstant::ONLINE_1 ? DriverConstant::ONLINE_0 : DriverConstant::ONLINE_1;
        $user->save();
        $this->success($user->is_online == DriverConstant::ONLINE_1 ? '关闭成功' : '开启成功');
    }

    /**
     * 更新定位（10秒/次）
     *
     * @ApiMethod (POST)
     * @ApiParams (name="lng", type="float", required=true, description="司机位置经度 118.387103")
     * @ApiParams (name="lat", type="float", required=true, description="司机位置纬度 35.057649")
     * @ApiParams (name="lng_lat", type="string", required=false, description="司机位置经纬度 118.387103,35.057649")
     * @ApiParams (name="order_id", type="string", required=false, description="订单号")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code": 1,"msg": "更新成功","time": "1694660842","data": null})
     **/
    public function location()
    {
        $lng = $this->request->post('lng');
        $lat = $this->request->post('lat');
        $lng_lat = $this->request->post('lng_lat');
        $order_id = $this->request->post('order_id');
        $user = $this->auth->getUser();
        $params = compact('lng', 'lat');
        DriverService::location($params, $lng_lat, $order_id, $user);
        $this->success('更新成功');
    }

    /**
     * 意见反馈
     *
     * @ApiMethod (POST)
     * @ApiParams (name="mobile", type="string", required=true, description="手机号")
     * @ApiParams (name="content", type="string", required=true, description="内容")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"提交成功","time":"1721783135","data":null})
     **/
    public function feedback(){
        $mobile = $this->request->post('mobile');
        $content = $this->request->post('content');
        $params = compact('mobile','content');
        $user = $this->auth->getUser();
        DriverService::feedback($params,$user);
        $this->success('提交成功');
    }

}
